package bb.lanxing.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import bb.lanxing.R;


public class AccountInputView extends LinearLayout {
    private ImageView deleteEditText;
    private EditText editText;
    private int iconBg;
    private ImageView imageView;
    private int inputType;
    private String textHint;

    public AccountInputView(Context context) {
        this(context, null);
    }

    public AccountInputView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public AccountInputView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.AccountInputView, i, 0);
        this.iconBg = obtainStyledAttributes.getResourceId(R.styleable.AccountInputView_iconBg, 0);
        this.textHint = obtainStyledAttributes.getString(R.styleable.AccountInputView_textHint);
        this.inputType = obtainStyledAttributes.getInt(R.styleable.AccountInputView_inputType, 32);
        obtainStyledAttributes.recycle();
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.account_input_item, this);
        this.imageView = findViewById(R.id.imageView);
        this.editText = findViewById(R.id.editText);
        this.deleteEditText = findViewById(R.id.img_del_phone);
        this.imageView.setBackgroundResource(this.iconBg);
        this.editText.setHint(this.textHint);
        this.editText.setHintTextColor(Color.parseColor("#bdbdbd"));
        this.editText.setInputType(this.inputType);
        this.editText.setOnFocusChangeListener((view, focus) -> {
            if (AccountInputView.this.editText.getText().length() <= 0 || !focus) {
                AccountInputView.this.deleteEditText.setVisibility(View.GONE);
            } else {
                AccountInputView.this.deleteEditText.setVisibility(View.VISIBLE);
            }
        });
        this.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
            }

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                int length = AccountInputView.this.editText.getText().length();
                Log.d("AccountInputView", "onTextChanged size = " + length);
                if (length > 0) {
                    AccountInputView.this.deleteEditText.setVisibility(View.VISIBLE);
                } else {
                    AccountInputView.this.deleteEditText.setVisibility(View.GONE);
                }
            }
        });
        this.deleteEditText.setOnClickListener(view -> {
            AccountInputView.this.editText.setText("");
            AccountInputView.this.deleteEditText.setVisibility(View.GONE);
        });
    }

    public String getText() {
        return this.editText.getText().toString();
    }

    public void setText(String str) {
        this.editText.setText(str);
    }

    public void setText(int i) {
        this.editText.setText(i);
    }

    public ImageView getImageView() {
        return this.imageView;
    }

    public EditText getEditText() {
        return this.editText;
    }
}
